package com.lucanjun.cn.springsecurity.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author lucanjun
 * @create 2025-03-10
 * @describe 统一响应结果集
 */
@Data
@NoArgsConstructor
@Schema(name = "HttpResult", description = "统一响应结果集")
public class HttpResult<T> implements Serializable {

    private static final long serialVersionUID = 3854679720587282344L;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "消息")
    private String message;

    @Schema(description = "详情")
    private String detail;

    @Schema(description = "返回数据")
    private T data;

    public HttpResult(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    private HttpResult(T data) {
        this.data = data;
        this.status = HttpResultCodeEnum.SUCCESS.getCode();
        this.message = HttpResultCodeEnum.SUCCESS.getMessage();
    }

    private HttpResult(Integer status, String message) {
        this.status = status;
        this.message = message;
    }

    public static <T> HttpResult<T> returnSuccess() {
        return new HttpResult<>(null);
    }

    public static <T> HttpResult<T> returnSuccess(T data) {
        return new HttpResult<>(data);
    }

    public static <T> HttpResult<T> returnSuccess(String message, T data) {
        return new HttpResult<>(HttpResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> HttpResult<T> returnSuccess(Integer status, String message, T data) {
        return new HttpResult<T>(status, message, data);
    }

    public static <T> HttpResult<T> returnFail() {
        return new HttpResult<>(HttpResultCodeEnum.FAIL.getCode(), null);
    }

    public static <T> HttpResult<T> returnFail(HttpResultCodeEnum responseCode) {
        return new HttpResult<>(responseCode.getCode(), responseCode.getMessage());
    }


    public static <T> HttpResult<T> returnFail(String message, T data) {
        return new HttpResult<>(HttpResultCodeEnum.FAIL.getCode(), message, data);
    }

    public static <T> HttpResult<T> returnFail(Integer code, String message) {
        return new HttpResult<>(code, message, null);
    }

    public static <T> HttpResult<T> returnFail(String message) {
        return new HttpResult<>(HttpResultCodeEnum.FAIL.getCode(), message, null);
    }

    public static <T> HttpResult<T> returnFail(Integer code, String message, T data) {
        return new HttpResult<>(code, message, data);
    }

}